import os
import tkinter as tk
from tkinter import filedialog

def open_image_file_dialog() -> str:
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename(title="选择要处理的图片",
                                           filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp *.gif *.tif")])
    return file_path

def save_image_file_dialog(original_path) -> str:
    """
    打开保存文件对话框，并尝试自动命名。
    如果 original_path 提供了原始文件路径，则基于该文件名生成新名称。
    """
    root = tk.Tk()
    root.withdraw()
    # 构建默认文件名
    if original_path and os.path.isfile(original_path):
        dir_name = os.path.dirname(original_path)
        base_name = os.path.splitext(os.path.basename(original_path))[0]
        ext = os.path.splitext(original_path)[1]
        new_file_name = f"{base_name}(1){ext}"
        initial_dir = dir_name
        initial_file = new_file_name
    else:
        dir_name = os.getcwd() if os.path.isdir(os.getcwd()) else None
        initial_dir = dir_name
        initial_file = ""
    file_path = filedialog.asksaveasfilename(
        defaultextension=".png",
        title="保存处理好的图片",
        filetypes=[("PNG file", "*.png"), ("JPEG file", "*.jpg *.jpeg"),
                   ("BMP file", "*.bmp"), ("GIF file", "*.gif"), ("TIFF file", "*.tif")],
        initialdir=initial_dir,
        initialfile=initial_file
    )
    return file_path